#include "friendmanager.h"

// ========== FriendItem 实现 ==========
FriendItem::FriendItem(const QString& avatarPath, const QString& name,
                       bool isOnline, const QString& signature, QWidget* parent)
    : QWidget(parent) {
    // 布局：头像 + 右侧（昵称 + 状态 + 签名）
    QHBoxLayout* mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(10, 5, 10, 5);
    mainLayout->setSpacing(10);

    // 头像
    m_avatarLabel = new QLabel(this);
    QPixmap avatar(avatarPath);
    m_avatarLabel->setPixmap(avatar.scaled(40, 40, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    m_avatarLabel->setFixedSize(40, 40);
    mainLayout->addWidget(m_avatarLabel);

    // 右侧信息（垂直布局）
    QVBoxLayout* rightLayout = new QVBoxLayout();
    rightLayout->setContentsMargins(0, 0, 0, 0);
    rightLayout->setSpacing(3);

    // 昵称
    m_nameLabel = new QLabel(name, this);
    m_nameLabel->setStyleSheet("font-weight: bold; color: #333;");
    rightLayout->addWidget(m_nameLabel);

    // 状态
    m_statusLabel = new QLabel(this);
    m_statusLabel->setText(isOnline ? "[在线]" : "[离线]");
    m_statusLabel->setStyleSheet("color: #666; font-size: 12px;");
    rightLayout->addWidget(m_statusLabel);

    // 签名
    m_signatureLabel = new QLabel(signature, this);
    m_signatureLabel->setStyleSheet("color: #999; font-size: 12px;");
    rightLayout->addWidget(m_signatureLabel);

    mainLayout->addLayout(rightLayout);
    mainLayout->addStretch(); // 右侧拉伸，使内容靠左
}

void FriendItem::paintEvent(QPaintEvent* event) {
    QWidget::paintEvent(event);
    // 绘制 hover 效果（鼠标悬浮时背景变色）
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

// ========== FriendManager 实现 ==========
FriendManager::FriendManager(QWidget* parent) : QMainWindow(parent) {
    initUI();
    initStyle();
}

void FriendManager::initUI() {
    // 中央窗口：左侧树 + 右侧堆叠窗口
    QWidget* centralWidget = new QWidget(this);
    QHBoxLayout* centralLayout = new QHBoxLayout(centralWidget);
    centralLayout->setContentsMargins(0, 0, 0, 0);
    centralLayout->setSpacing(0);

    // ---------- 左侧：分组树 ----------
    m_treeWidget = new QTreeWidget(this);
    m_treeWidget->setHeaderHidden(true); // 隐藏表头
    m_treeWidget->setColumnCount(1);
    centralLayout->addWidget(m_treeWidget, 1); // 占 1 份宽度

    // 初始化分组树
    initTreeWidget();

    // ---------- 右侧：堆叠窗口（切换不同分组的好友列表） ----------
    m_stackedWidget = new QStackedWidget(this);
    centralLayout->addWidget(m_stackedWidget, 3); // 占 3 份宽度

    // 设置中央窗口
    setCentralWidget(centralWidget);
    resize(800, 600); // 初始大小
    setWindowTitle("好友管理器");
}

void FriendManager::initTreeWidget() {
    // 1. 我的设备
    QTreeWidgetItem* deviceItem = new QTreeWidgetItem(m_treeWidget);
    deviceItem->setText(0, "我的设备");
    QTreeWidgetItem* phoneItem = new QTreeWidgetItem(deviceItem);
    phoneItem->setText(0, "我的手机");
    m_treeWidget->addTopLevelItem(deviceItem);

    // 2. 机器人
    QTreeWidgetItem* robotItem = new QTreeWidgetItem(m_treeWidget);
    robotItem->setText(0, "机器人");
    QTreeWidgetItem* robotSubItem = new QTreeWidgetItem(robotItem);
    robotSubItem->setText(0, "机器人1");
    m_treeWidget->addTopLevelItem(robotItem);

    // 3. 特别关心
    QTreeWidgetItem* specialItem = new QTreeWidgetItem(m_treeWidget);
    specialItem->setText(0, "特别关心");
    QTreeWidgetItem* specialSubItem = new QTreeWidgetItem(specialItem);
    specialSubItem->setText(0, "特别关心1");
    m_treeWidget->addTopLevelItem(specialItem);

    // 4. 我的好友
    QTreeWidgetItem* friendItem = new QTreeWidgetItem(m_treeWidget);
    friendItem->setText(0, "我的好友");
    QTreeWidgetItem* friendSubItem1 = new QTreeWidgetItem(friendItem);
    friendSubItem1->setText(0, "好友1");
    QTreeWidgetItem* friendSubItem2 = new QTreeWidgetItem(friendItem);
    friendSubItem2->setText(0, "好友2");
    m_treeWidget->addTopLevelItem(friendItem);

    // 展开所有分组
    m_treeWidget->expandAll();

    // 点击分组，切换右侧好友列表
    connect(m_treeWidget, &QTreeWidget::currentItemChanged,
            this, [this](QTreeWidgetItem* current) {
        if (!current) return;
        // 简化逻辑：根据分组名切换不同的好友列表
        QString text = current->text(0);
        if (text == "我的手机") {
            QListWidget* list = createFriendList();
            m_stackedWidget->addWidget(list);
            m_stackedWidget->setCurrentWidget(list);
        } else if (text == "机器人1") {
            QListWidget* list = createFriendList();
            m_stackedWidget->addWidget(list);
            m_stackedWidget->setCurrentWidget(list);
        } else if (text == "特别关心1") {
            QListWidget* list = createFriendList();
            m_stackedWidget->addWidget(list);
            m_stackedWidget->setCurrentWidget(list);
        } else if (text == "好友1") {
            QListWidget* list = createFriendList();
            m_stackedWidget->addWidget(list);
            m_stackedWidget->setCurrentWidget(list);
        }
    });
}

QListWidget* FriendManager::createFriendList() {
    QListWidget* listWidget = new QListWidget();
    listWidget->setStyleSheet("QListWidget { border: none; }");
    listWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);

    // 示例：添加几个好友项
    FriendItem* item1 = new FriendItem(":/images/avatar1.png", "谪仙",
                                       true, "礼乐征伐自天子(吾)出", listWidget);
    FriendItem* item2 = new FriendItem(":/images/avatar2.png", "大讲堂-助教 (Yiksun)",
                                       true, "我为你歌唱，感谢你的恩典……", listWidget);
    FriendItem* item3 = new FriendItem(":/images/avatar3.png", "物联网231班校草虞貝睿 (灵感菇)",
                                       true, "哈吉嘛", listWidget);

    QListWidgetItem* listItem1 = new QListWidgetItem(listWidget);
    listItem1->setSizeHint(item1->sizeHint());
    listWidget->setItemWidget(listItem1, item1);

    QListWidgetItem* listItem2 = new QListWidgetItem(listWidget);
    listItem2->setSizeHint(item2->sizeHint());
    listWidget->setItemWidget(listItem2, item2);

    QListWidgetItem* listItem3 = new QListWidgetItem(listWidget);
    listItem3->setSizeHint(item3->sizeHint());
    listWidget->setItemWidget(listItem3, item3);

    return listWidget;
}

void FriendManager::initStyle() {
    // 全局样式表
    setStyleSheet(R"(
        QMainWindow {
            background-color: #f5f5f5;
        }
        QTreeWidget {
            background-color: #fff;
            border-right: 1px solid #ddd;
            color: #333;
        }
        QTreeWidget::item {
            height: 30px;
        }
        QTreeWidget::item:selected {
            background-color: #e6f7ff;
            color: #0078d7;
        }
        QListWidget {
            background-color: #fff;
        }
        FriendItem:hover {
            background-color: #f0f0f0;
        }
    )");
}
